package com.util;

/*import cn.xclink.common.utils.DateUtils;
import cn.xclink.common.utils.StringUtils;
import cn.xclink.common.utils.security.ShiroUtils;
import cn.xclink.kernel.web.page.PageDomain;
import cn.xclink.kernel.web.page.TableDataInfo;
import cn.xclink.kernel.web.page.TableSupport;
import cn.xclink.project.system.user.domain.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;*/
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * web层通用数据处理
 * 
 * @author
 */
public class BaseController
{
    /**
     * 文件上传路径
     */
    public static final String UPLOAD_PATH = "/profile/upload/";
    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String text)
            {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    /**
     * 设置请求分页数据
     */
  /* protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = pageDomain.getOrderBy();
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }*/

    /**
     * 响应请求分页数据
     */
   /* @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        //System.out.println("**********************"+rspData.getRows());
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }*/

    @RequestMapping("/common/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest req, HttpServletResponse res) throws Exception
    {
        try
        {
//            // 上传文件路径
//            String filePath = AlphaConfig.getUploadPath();
             String filePath=req.getServletContext().getRealPath("imgs");
//            // 上传并返回新文件名称
//            String fileName = FileUploadUtils.upload(filePath, file);
            String fileName=file.getOriginalFilename();
//            String url = "http://image.hmjs.group" + UPLOAD_PATH + fileName;
            String url = "http://image.hmjs.group" + UPLOAD_PATH + fileName;
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath+"/"+fileName));
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 响应返回结果
     * 
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? success() : error();
    }
    
    /**
     * 响应返回结果
     * 
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public AjaxResult error(int code, String message)
    {
        return AjaxResult.error(code, message);
    }

    /**
     * 页面跳转
     */
    /*public String redirect(String url)
    {
        return StringUtils.format("redirect:{}", url);
    }

    public User getSysUser()
    {
        return ShiroUtils.getSysUser();
    }

    public void setSysUser(User user)
    {
        ShiroUtils.setSysUser(user);
    }

    public Long getUserId()
    {
        return getSysUser().getUserId();
    }

    public String getLoginName()
    {
        return getSysUser().getLoginName();
    }*/
}
